package com.example.demo.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    static public DateUtil getDate(){
        return new DateUtil();
    }
    /***
     * 获取days天之前的日期
     * @param days
     * @return
     */
    public String getDateBeforeToday(int days)
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar = simpleDateFormat.getCalendar();
        try {
            Date date = simpleDateFormat.parse(getDateNow());
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -days);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return simpleDateFormat.format(calendar.getTime());
        }

    }
    public boolean isToday(String dateString)
    {
        SimpleDateFormat simpleDateFormat=null;
        Date dateNow=null;
        Date date=null;
        if (dateString.length() > 10) {
            dateString = dateString.substring(0, 10);
        }


//        System.out.println("date is "+ date.getTime()/86400000);
//        System.out.println("dateNow is "+ dateNow.getTime()/86400000);

        return dateString.equals(getDateNow());

    }

    /***
     * 日期不比今天大
     * @param dateString
     * @return
     */
    public boolean isNotBigerThanToday(String dateString )
    {
        SimpleDateFormat simpleDateFormat=null;
        //Calendar calendarNow=null;
        //Calendar calendar=null;

        Date date1=null;
        Date dateNow=null;
        try {
            if (dateString.length() > 10) {
                //取整  删去时间部分 只保留日期
                dateString=dateString.substring(0,10);
            }

            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            date1 = simpleDateFormat.parse(dateString);
            dateNow =simpleDateFormat.parse(getDateTimeNow());


        }

        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        System.out.println(dateString +" (long:" +date1.getTime()+")is  "+(date1.compareTo(dateNow)) +" lower than now:"+getDateTimeNow()+"(long:"+dateNow.getTime()+")");

        return date1.compareTo(dateNow)<1;
    }

    public boolean isLast7Days(String dateString)
    {
        return isIn(getDateNow(),dateString,7);
    }
    public boolean isLast30Days(String dateString)
    {
        return  isIn(getDateNow(),dateString,30);
    }

    public String getDateAfter(String dateString,int days) {
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar calendar = simpleDateFormat.getCalendar();
        try {
            Date date = simpleDateFormat.parse(dateString);
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, days);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return simpleDateFormat.format(calendar.getTime());
        }
    }

    /***
     * 获取当前日期 精确到日
     * @return
     */
    public String getDateNow()
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date dateNow=new Date();

        return  simpleDateFormat.format(dateNow.getTime());
    }

    /**
     * 获取当前日期，精确到秒
     * @return
     */
    public String getDateTimeNow()
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateNow=new Date();

        return  simpleDateFormat.format(dateNow.getTime());
    }


    /***
     *获取给定2个日期相差的天数
     * @param dateString1
     * @param dateString2
     * @return
     */
    public long  getDifferDayas(String dateString1,String dateString2)
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date1=null;
        Date date2=null;
        try {
            date1=simpleDateFormat.parse(dateString1);
            date2=simpleDateFormat.parse(dateString2);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        long differDays=(date2.getTime()-date1.getTime())/86400000;
//        System.out.println(dateString1 +" and " +dateString2 +" is lower differDays :"+differDays);
        return differDays;

    }

    /***
     * 比较2个日期字符串相差天数是否等于指定天数
     * @param dateString1
     * @param dateString2
     * @param number
     * @return
     */
    public boolean isDifferDays(String dateString1, String dateString2, long number)
    {
        long differDays=getDifferDayas(dateString1,dateString2);

        if(differDays==number)
        {
            System.out.println(dateString1+" and "+dateString2);
            return  true;
        }
        else {
            System.out.println(dateString1+" no "+dateString2);
            return false;
        }
    }

    /***
     * 比较两个时间相差多少分钟
     * @param dateString1
     * @param dateString2
     * @return
     */
    public long getDifferMinutes(String dateString1, String dateString2)
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1=null;
        Date date2=null;
        try {
            date1=simpleDateFormat.parse(dateString1);
            date2=simpleDateFormat.parse(dateString2);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        long differMinutes=(date2.getTime()-date1.getTime())/60000;
//        System.out.println("differMinutes :"+differMinutes);
        return  differMinutes;

    }
    //获取给定时间与当前时间相差多少分钟
    public long getDifferMiniutesFromNow(String dateString)
    {
        return getDifferMinutes(dateString,getDateTimeNow());
    }
    /***
     * 比较dateString 与今天日期相差天数是否等于指定天数
     * @param dateString
     * @param number
     * @return
     */
    public boolean isDifferFromToday(String dateString,long number)
    {
        return isDifferDays(getDateNow(),dateString,number);
    }

    /**
     * 比较2个日期字符串的相差天数是否小于等于指定天数
     * @param dateString1
     * @param dateString2
     * @param number
     * @return
     */
    public boolean isIn(String dateString1,String dateString2,long number)
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date1=null;
        Date date2=null;
        try {
            date1=simpleDateFormat.parse(dateString1);
            date2=simpleDateFormat.parse(dateString2);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        long differDays=(date2.getTime()-date1.getTime())/86400000;
//        System.out.println("differDates :"+differDays);

        if(differDays<=number&&differDays>=0)
        {
            System.out.println(dateString1+" and "+dateString2);
            return  true;
        }
        else {
            System.out.println(dateString1+" no "+dateString2);
            return false;
        }
    }

    public int compare(String dateString1,String dateString2)
    {
        SimpleDateFormat simpleDateFormat=null;

        Date date1=null;
        Date date2=null;
        try {
            if (dateString1.length() > 10) {
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


            } else {
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            }


            date1 = simpleDateFormat.parse(dateString1);
            date2 = simpleDateFormat.parse(dateString2);
        }

        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        return date1.compareTo(date2);

    }

    /**
     * 判断dateString与今天相差的天数是否小于等于指定参数
     * @param dateString
     * @param number
     * @return
     */
    public boolean isInDaysFromToday(String dateString, long number)
    {
        return isIn(getDateNow(),dateString,number);

    }
}